/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package info.abdolahi.slice;

import info.abdolahi.ResourceTable;
import info.abdolahi.CircularMusicProgressBar;
import info.abdolahi.LogUtil;
import info.abdolahi.OnCircularSeekBarChangeListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

import java.security.SecureRandom;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = "MainActivity";
    /**
     * Progress bar
     */
    CircularMusicProgressBar progressBar;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        progressBar = (CircularMusicProgressBar) findComponentById(ResourceTable.Id_album_art);
        // set progress to 40%
        progressBar.setValue(40);
        // get user update
        progressBar.setOnCircularBarChangeListener(new OnCircularSeekBarChangeListener() {
            @Override
            public void onProgressChanged(CircularMusicProgressBar circularBar, int progress, boolean fromUser) {
                LogUtil.info(TAG, "onProgressChanged: progress: " + progress + " / from user? " + fromUser);
            }

            @Override
            public void onClick(CircularMusicProgressBar circularBar) {
                LogUtil.info(TAG, "onClick");
                updateRandomly();
            }

            @Override
            public void onLongPress(CircularMusicProgressBar circularBar) {
                LogUtil.info(TAG, "onLongPress");
            }

        });
        progressBar.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtil.info(TAG, "onClick setClickedListener");
                updateRandomly();
            }
        });
    }

    private void updateRandomly() {
        SecureRandom random = new SecureRandom();
        final float percent = random.nextFloat() * 100;
        progressBar.setValue(percent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
